package com.liusaidh.controller;
import com.liusaidh.domain.*;
import com.liusaidh.service.BlogService;
import com.liusaidh.service.FansService;
import com.liusaidh.service.UserService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 刘志文
 */
@Controller
@Api(tags = "用户登录和注册、关注/取消关他人的相关的操作")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private FansService fansService;
    @Resource
    private BlogService blogService;


    @GetMapping("/")
    public String index(HttpSession session) {
        ServletContext application = session.getServletContext();
        application.setAttribute("email", "************");
        application.setAttribute("code", "************");
        return "redirect:/blog/getIndexBlog?state=1";
    }


    @ApiOperation(value = "注册", notes = "用户注册，要传来三个参数")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "1.邮箱回显：email /2.信息提醒：msg/3.密码回显：pwd/4.验证码回显：code", response = String.class)})
    @PostMapping("/user/register")
    public ModelAndView register(@ApiParam(value = "接收用户输入的验证码") @RequestParam(name = "register-code") String code,
                                 @ApiParam(value = "接收用户输入的邮箱") @RequestParam("register-email") String email,
                                 @ApiParam(value = "接收用户输入的密码") @RequestParam("register-password") String password,
                                 @ApiParam(value = "用于获取session中的验证码和邮箱") HttpSession session) {
       return userService.register(code,email,password,session);
    }

    @ApiOperation(value = "获取验证码", notes = "不返回任何信息，仅仅点击获取验证码时，向用户发送验证码")
    @PostMapping("/user/getCode")
    @ResponseBody
    public void getCode(@RequestParam("email") String email, HttpSession session) {
        String code = userService.getEmailRegister(email);
        session.getServletContext().setAttribute("code", code);
        session.getServletContext().setAttribute("email", email);
    }

    @ApiOperation(value = "登录", notes = "用户登录", response = String.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "1.邮箱回显：email /2.信息提醒：msg", response = String.class)
    })
    @PostMapping("/user/login")
    public ModelAndView login(@ApiParam(value = "用户输入的邮箱") @RequestParam("login-email") String email,
                              @ApiParam(value = "用户输入的密码") @RequestParam("login-password") String password,
                              @ApiParam(value = "用于存放用户的id") HttpSession session) {

       return userService.login(email,password,session);
    }

    @ApiOperation(value = "用户关注他人", notes = "用户关注其他的用户,无返回值", response = String.class)
    @PostMapping("/user/concernOtherUser")
    @ResponseBody
    public String concernOtherUser(@ApiParam(name = "id", value = "被关注用户的id", required = true) @RequestParam("id") Integer id,
                                   @ApiParam(value = "用于存放登录的用户的id") HttpSession session) {
        userService.concernUser((Integer) session.getAttribute("id"), id);
        return "关注成功！";
    }

    @ApiOperation(value = "用户取消关注他人", notes = "用户取消关注其他的用户", response = String.class)
    @PostMapping("/user/notConcernOtherUser")
    @ResponseBody
    public String notConcernOtherUser(@ApiParam(value = "被关注用户的id", required = true) @RequestParam("id") Integer id,
                                      @ApiParam(value = "用于存放登录的用户的id") HttpSession session) {
        userService.notConcernUser((Integer) session.getAttribute("id"), id);
        return "取消关注成功！";
    }

    @ApiOperation(value = "搜索用户后跳转到个人中心的页面", notes = "1.user:用户信息；2.fansNum：粉丝的数量；3.idolsNum：关注的数量4.myBlog：我的文章5.hotSearchBlog：右边的热搜")
    @GetMapping("/user/toOnesInfo")
    public ModelAndView toOnesInfo(@ApiParam(name = "id", value = "要跳转的用户的id值") @RequestParam("id") Integer id) {
        ModelAndView mav = new ModelAndView("personalPage");
        User user = userService.getUserById(id);
        List<MyBlog> myBlog = blogService.getAllMyBlog(id, 1, 10);
        List<String> hotSearchBlog = blogService.getTenHotSearchBlog();
        mav.addObject("allType", blogService.getTypeByLike(null));
        mav.addObject("user", user);
        mav.addObject("fansNum", fansService.getMyFansListSize(id));
        mav.addObject("idolsNum", fansService.getMyIdolsListSize(id));
        mav.addObject("myBlog", myBlog);
        mav.addObject("hotBlog", hotSearchBlog);
        mav.addObject("color", "black");
        return mav;
    }


    @ApiOperation(value = "ajax搜索用户后跳转到个人中心的页面", notes = "1.user:用户信息；2.fansNum：粉丝的数量；3.idolsNum：关注的数量4.myBlog：我的文章5.hotSearchBlog：右边的热搜")
    @GetMapping("/user/toLoadedOnesInfo")
    @ResponseBody
    public List<MyBlog> toLoadedOnesInfo(@ApiParam(name = "id", value = "要跳转的用户的id值") @RequestParam("id") Integer id,
                                         @ApiParam(name = "pageNo", value = "当前的页码数") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @ApiParam(name = "pageSize", value = "每页的数量") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return blogService.getAllMyBlog(id, pageNo, pageSize);
    }


    @ApiOperation(value = "该接口用来判断该用户是否关注其他的用户", notes = "用来判断该用户是否关注其他的用户，传来其他用户的id,返回true代表关注，false代表未关注", response = boolean.class)
    @PostMapping("/user/judgeUserOfConnection")
    @ResponseBody
    public boolean judgeUserOfConnection(@ApiParam(value = "其他用户的id值", required = true) @RequestParam("id") Integer id,
                                         @ApiParam(value = "用于存放登录的用户的id") HttpSession session) {
        return userService.judgeUserOfConnection((Integer) session.getAttribute("id"), id);
    }

    @ApiOperation(value = "得到别人对我的评论", notes = "得到其他人对我的评论，1.返回一个list集合，里面放的是一个个Comment对象2.返回一个hotSearchBlog，里面放的是页面右边的热搜", response = Comment.class)
    @GetMapping("/user/getOthersCommentOfMe")
    public ModelAndView getOthersCommentOfMe(@ApiParam(value = "用于存放登录的用户的id") HttpSession session) {
        ModelAndView mav = new ModelAndView("message-comment");
        List<Comment> list = userService.getOthersCommentOfMe((Integer) session.getAttribute("id"), 1, 10);
        List<String> hotSearchBlog = blogService.getTenHotSearchBlog();
        mav.addObject("hotSearchBlog", hotSearchBlog);
        mav.addObject("list", list);
        mav.addObject("allType", blogService.getTypeByLike(null));
        return mav;
    }

    @ApiOperation(value = "ajax得到别人对我的评论", notes = "得到其他人对我的评论", response = Comment.class)
    @GetMapping("/user/getLoadedOthersCommentOfMe")
    @ResponseBody
    public List<Comment> getLoadedOthersCommentOfMe(@ApiParam(value = "用于存放登录的用户的id") HttpSession session,
                                                    @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userService.getOthersCommentOfMe((Integer) session.getAttribute("id"), pageNo, pageSize);
    }

    @ApiOperation(value = "得到别人对我的点赞", notes = "得到其他人对我的点赞，1.返回一个list集合，里面放的是一个个Like对象2.返回一个hotSearchBlog，里面放的是页面右边的热搜", response = Like.class)
    @GetMapping("/user/getOthersLikeOfMe")
    public ModelAndView getOthersLikeOfMe(@ApiParam(value = "用于存放登录的用户的id") HttpSession session) {
        ModelAndView mav = new ModelAndView("message-like");
        mav.addObject("allType", blogService.getTypeByLike(null));
        List<String> hotSearchBlog = blogService.getTenHotSearchBlog();
        List<Like> list = userService.getOthersLikeOfMe((Integer) session.getAttribute("id"), 1, 10);
        mav.addObject("list", list);
        mav.addObject("id", ((Integer) session.getAttribute("id")));
        mav.addObject("hotSearchBlog", hotSearchBlog);
        return mav;
    }

    @ApiOperation(value = "ajax得到别人对我的点赞", notes = "返回一个list集合，里面放的是一个个Like对象", response = Like.class)
    @GetMapping("/user/getLoadedOthersLikeOfMe")
    @ResponseBody
    public List<Like> getLoadedOthersLikeOfMe(@ApiParam(value = "用于存放登录的用户的id") HttpSession session,
                                              @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userService.getOthersLikeOfMe((Integer) session.getAttribute("id"), pageNo, pageSize);
    }


    @ApiOperation(value = "获取当前登录用户的信息", notes = "获取当前登录用户的信息", response = User.class)
    @GetMapping("/user/getLoginUserInfo")
    @ResponseBody
    public User getLoginUserInfo(@ApiParam(value = "用于存放登录的用户的id") HttpSession session) {
        return userService.getUserById((Integer) session.getAttribute("id"));
    }

    @ApiOperation(value = "用户退出登录", notes = "用户退到登录界面")
    @GetMapping("/user/logout")
    public ModelAndView userLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("id");
        ModelAndView mav = new ModelAndView("redirect:/blog/getIndexBlog?state=1");
        return mav;
    }
}
